﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;
public partial class AS_ShowCommon : BasePage
{
    private readonly UserFavoriteManage _userFavorite = new UserFavoriteManage();
    private readonly ShoppingCartsManage _shoppingCartsManage = new ShoppingCartsManage();
    private readonly GoodsManage _goodManage = new GoodsManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var action = Request.QueryString["Action"] + "";
            switch (action.ToLower())
            {
                case "addfavorite":
                    AddFavorite();
                    break;
                case "addshopping":
                    AddShopping();
                    break;
                case "commendbuy":
                    CommendBuy();
                    break;
            }
            Response.End();
        }
    }
    public void CommendBuy()
    {
        var goodid = Utility.G<string>("GoodsID");
        var goodslist = goodid.Split(',');

        var quantity = 1;
        var discount = 1;
        var result = new ReturnValue();
        for (var i = 0; i < goodslist.Count(); i++)
        {
            var goodsinfo = _goodManage.GetGoods(Convert.ToInt32(goodslist[i]));
            ShoppingCarts shoppingCart = new ShoppingCarts();
            shoppingCart.CreateDate = DateTime.Now;
            shoppingCart.GoodsID = goodsinfo.BaseInfo.GoodsID;
            shoppingCart.CookiesID = _shoppingCartsManage.GetShoppingCartCookieName(this.UserID); ;
            shoppingCart.Discount = discount;
            shoppingCart.PresentScores = goodsinfo.BaseInfo.GiveScores;
            shoppingCart.Price = goodsinfo.BaseInfo.SalePrice;
            shoppingCart.Quantity = quantity;
            result = _shoppingCartsManage.Save(shoppingCart);
        }
        
        Response.Write(JSONHelper.ObjectToJson(result));
    }

    private void AddFavorite()
    {
        var goodid = Utility.G<int>("GoodsID");
        ReturnValue ret = _userFavorite.Add(this.UserID, goodid);
        Response.Write(JSONHelper.ObjectToJson(ret));
    }

    private void AddShopping()
    {
        var goodid = Utility.G<int>("GoodsID");
        var quantity = Utility.G<int>("Quantity");
        var discount = Utility.G<decimal>("Discount");
        
        var goodsinfo = _goodManage.GetGoods(goodid);
        ShoppingCarts shoppingCart = new ShoppingCarts();
        shoppingCart.CreateDate = DateTime.Now;
        shoppingCart.GoodsID = goodid;
        shoppingCart.CookiesID = _shoppingCartsManage.GetShoppingCartCookieName(this.UserID);
        shoppingCart.Discount = discount;
        shoppingCart.PresentScores = goodsinfo.BaseInfo.GiveScores;
        shoppingCart.Price = goodsinfo.BaseInfo.SalePrice;
        shoppingCart.Quantity = quantity;
        var result = _shoppingCartsManage.Save(shoppingCart);
        Response.Write(JSONHelper.ObjectToJson(result));
    }
}